﻿
using HtmlAgilityPack;
using Newtonsoft.Json;
using RTW.OA.Areas.System.Servers;
using RTW.OA.Models;
using RTW.Utils.Utils;
using System;
using System.Collections.Generic;
using System.Web.Mvc;

namespace RTW.OA.Areas.System.Controllers
{
    public class FormController : Controller
    {
        private FormServers formServers;

        public FormController()
        {
            formServers = new FormServers();
        }


        // GET: System/Form
        public ActionResult Index()
        {
            ViewBag.TYPEIDData = JsonConvert.SerializeObject(formServers.GetTYPEID());
            return View();
        }

        // GET: System/Form/Edit/5
        public ActionResult Edit(int? id)
        {
            ViewBag.TYPEIDData = formServers.GetTYPEID();
            var model = new wf_forminfo();
            if (id != null)
            {
                ViewBag.Title = "编辑表单";
                model = GetModel(id.Value.ToString());
            }
            else
            {
                ViewBag.Title = "新建表单";
            }
            return View(model);
        }

        // POST: System/Form/Edit/5
        [HttpPost]
        public ActionResult Edit(int? id, wf_forminfo model)
        {
            ViewBag.TYPEIDData = formServers.GetTYPEID();
            try
            {
                if (id == null || id == 0)
                {
                    formServers.Add(model);
                    ViewBag.msg = "新增表单信息成功！";
                    model = new wf_forminfo();

                }
                else
                {
                    var m = formServers.GetModelById(id.Value.ToString());
                    m.TYPEID = model.TYPEID;
                    m.NAME = model.NAME;
                    m.STATUS = model.STATUS;
                    formServers.Update(m);
                    ViewBag.msg = "修改表单信息成功！";
                }
            }
            catch 
            {
                ViewBag.msg = "提交出现错误！";
            }

            return View(model);
        }

        // GET: System/Form/Delete/5
        public ActionResult Delete(int id)
        {
            if (id != 0)
            {
                formServers.Del(id.ToString());
                ViewBag.msg = "删除表单信息成功！";
            }
            return Content("");
        }


        // GET: System/Form/Edit/5
        public ActionResult Formdesign(int? id)
        {
            ViewBag.Content = "";
            if (id != null)
            {
                ViewBag.Title = "设计表单";
                wf_forminfo model = GetModel(id.Value.ToString());
                ViewBag.Content = model.CONTENT;
            }
            else
            {
                ViewBag.Title = "新建表单";
            }
            return View();
        }

        [HttpPost]
        [ValidateInput(false)]
        public ActionResult Formdesign(int? id, FormCollection formCollection)
        {
            ViewBag.Content = "";
            if (id != null)
            {
                ViewBag.Title = "设计表单";
                wf_forminfo model = GetModel(id.Value.ToString());
                model.CONTENT = formCollection["design_content"];
                
                HtmlDocument htmlDocument = new HtmlDocument();
                htmlDocument.LoadHtml(model.CONTENT);
                int num = 0;
                string text = "";
                HtmlNodeCollection htmlNodeCollection = htmlDocument.DocumentNode.SelectNodes("//input");
                if (htmlNodeCollection != null)
                {
                    foreach (HtmlNode htmlNode in ((IEnumerable<HtmlNode>)htmlNodeCollection))
                    {
                        if (htmlNode.Attributes["leipiplugins"] != null)
                        {
                            if (htmlNode.Attributes["name"] != null && htmlNode.Attributes["name"].Value != "")
                            {
                                wf_formfieldinfo formFieldInfo = new wf_formfieldinfo();
                                if (htmlNode.Attributes["id"] != null)
                                {
                                    string num2 = htmlNode.Attributes["id"].Value;
                                    if (!string.IsNullOrEmpty(num2)&& int.Parse(num2) > 0)
                                    {
                                        if (formServers.GetModelFieldById(num2) != null)
                                        {
                                            formFieldInfo = formServers.GetModelFieldById(num2);
                                        }
                                    }
                                }
                                formFieldInfo.NAME = htmlNode.Attributes["name"].Value;
                                if (htmlNode.Attributes["orgtype"] != null)
                                {
                                    formFieldInfo.DATATYPE = htmlNode.Attributes["orgtype"].Value;
                                }
                                formFieldInfo.PLUGINS = htmlNode.Attributes["leipiplugins"].Value;
                                if (htmlNode.Attributes["value"] != null)
                                {
                                    formFieldInfo.DEFVALUE = htmlNode.Attributes["value"].Value;
                                }
                                if (formFieldInfo.ID > 0)
                                {
                                    formServers.UpdateField(formFieldInfo);                                   
                                }
                                else
                                {
                                    formFieldInfo.FORMID = model.ID;
                                    formServers.AddField(formFieldInfo);
                                    formFieldInfo.ID = formServers.GetIDByName(formFieldInfo.NAME, model.ID.ToString());
                                }
                                htmlNode.SetAttributeValue("id", formFieldInfo.ID.ToString());
                                num++;
                                if (text != "")
                                {
                                    text += ",";
                                }
                                text += formFieldInfo.ID.ToString();
                            }
                        }
                        else
                        {
                            htmlNode.SetAttributeValue("leipiplugins", "text");
                            if (htmlNode.Attributes["title"] != null && htmlNode.Attributes["title"].Value != "")
                            {
                                wf_formfieldinfo formFieldInfo = new wf_formfieldinfo();
                                //SqlParam[] sqlparams = new SqlParam[]
                                //{
                                //        DbHelper.MakeAndWhere("name", htmlNode.Attributes["title"].Value),
                                //        DbHelper.MakeAndWhere("formid", this.id)
                                //};
                                formFieldInfo = formServers.GetModelFieldByName(htmlNode.Attributes["title"].Value, id.Value.ToString());// DbHelper.ExecuteModel<FormFieldInfo>(sqlparams);
                                formFieldInfo.NAME = htmlNode.Attributes["title"].Value;
                                formFieldInfo.DATATYPE = "text";
                                formFieldInfo.PLUGINS = "text";
                                if (htmlNode.Attributes["value"] != null)
                                {
                                    formFieldInfo.DEFVALUE = htmlNode.Attributes["value"].Value;
                                }
                                if (formFieldInfo.ID > 0)
                                {
                                    formServers.UpdateField(formFieldInfo);
                                    //DbHelper.ExecuteUpdate<FormFieldInfo>(formFieldInfo);
                                }
                                else
                                {
                                    formFieldInfo.FORMID = model.ID;
                                    formServers.AddField(formFieldInfo);
                                    formFieldInfo.ID = formServers.GetIDByName(formFieldInfo.NAME, model.ID.ToString());
                                }
                                htmlNode.SetAttributeValue("name", formFieldInfo.NAME);
                                htmlNode.SetAttributeValue("id", formFieldInfo.ID.ToString());
                                num++;
                                if (text != "")
                                {
                                    text += ",";
                                }
                                text += formFieldInfo.ID.ToString();
                            }
                        }
                        if (htmlNode.Attributes["value"] == null)
                        {
                            htmlNode.SetAttributeValue("value", "");
                        }
                        if (htmlNode.Attributes["orghide"] == null)
                        {
                            htmlNode.SetAttributeValue("orghide", "0");
                        }
                        if (htmlNode.Attributes["orgalign"] == null)
                        {
                            htmlNode.SetAttributeValue("orgalign", "left");
                        }
                        if (htmlNode.Attributes["orgwidth"] == null)
                        {
                            htmlNode.SetAttributeValue("orgwidth", "");
                        }
                        if (htmlNode.Attributes["orgheight"] == null)
                        {
                            htmlNode.SetAttributeValue("orgheight", "");
                        }
                        if (htmlNode.Attributes["orgtype"] == null)
                        {
                            htmlNode.SetAttributeValue("orgtype", "text");
                        }
                    }
                }
                htmlNodeCollection = htmlDocument.DocumentNode.SelectNodes("//select");
                if (htmlNodeCollection != null)
                {
                    foreach (HtmlNode htmlNode in ((IEnumerable<HtmlNode>)htmlNodeCollection))
                    {
                        if (htmlNode.Attributes["leipiplugins"] != null)
                        {
                            if (htmlNode.Attributes["name"] != null && htmlNode.Attributes["name"].Value != "")
                            {
                                wf_formfieldinfo formFieldInfo = new wf_formfieldinfo();
                                if (htmlNode.Attributes["id"] != null)
                                {
                                    string num2 = htmlNode.Attributes["id"].Value;
                                    if (!string.IsNullOrEmpty(num2)&& int.Parse(num2) > 0)
                                    {
                                        if (formServers.GetModelFieldById(num2) != null)
                                        {
                                            formFieldInfo = formServers.GetModelFieldById(num2);
                                        }
                                    }
                                }
                                formFieldInfo.NAME = htmlNode.Attributes["name"].Value;
                                formFieldInfo.PLUGINS = htmlNode.Attributes["leipiplugins"].Value;
                                if (formFieldInfo.ID > 0)
                                {
                                    formServers.UpdateField(formFieldInfo);
                                }
                                else
                                {
                                    formFieldInfo.FORMID = model.ID;
                                    formServers.AddField(formFieldInfo);
                                    formFieldInfo.ID = formServers.GetIDByName(formFieldInfo.NAME, model.ID.ToString());
                                }
                                htmlNode.SetAttributeValue("id", formFieldInfo.ID.ToString());
                                num++;
                                if (text != "")
                                {
                                    text += ",";
                                }
                                text += formFieldInfo.ID.ToString();
                            }
                        }
                        else
                        {
                            htmlNode.SetAttributeValue("leipiplugins", "select");
                            if (htmlNode.Attributes["title"] != null && htmlNode.Attributes["title"].Value != "")
                            {
                                wf_formfieldinfo formFieldInfo = new wf_formfieldinfo();
                                //SqlParam[] sqlparams = new SqlParam[]
                                //{
                                //        DbHelper.MakeAndWhere("name", htmlNode.Attributes["title"].Value),
                                //        DbHelper.MakeAndWhere("formid", this.id)
                                //};
                                formFieldInfo = formServers.GetModelFieldByName(htmlNode.Attributes["title"].Value, id.Value.ToString());// DbHelper.ExecuteModel<FormFieldInfo>(sqlparams);
                                formFieldInfo.NAME = htmlNode.Attributes["title"].Value;
                                formFieldInfo.DATATYPE = "";
                                formFieldInfo.PLUGINS = "select";
                                if (formFieldInfo.ID > 0)
                                {
                                    formServers.UpdateField(formFieldInfo);
                                }
                                else
                                {
                                    formFieldInfo.FORMID = model.ID;
                                    formServers.AddField(formFieldInfo);
                                    formFieldInfo.ID = formServers.GetIDByName(formFieldInfo.NAME, model.ID.ToString());
                                }
                                htmlNode.SetAttributeValue("name", formFieldInfo.NAME);
                                htmlNode.SetAttributeValue("id", formFieldInfo.ID.ToString());
                                num++;
                                if (text != "")
                                {
                                    text += ",";
                                }
                                text += formFieldInfo.ID.ToString();
                            }
                        }
                        if (htmlNode.Attributes["orgwidth"] == null)
                        {
                            htmlNode.SetAttributeValue("orgwidth", "");
                        }
                    }
                }
                htmlNodeCollection = htmlDocument.DocumentNode.SelectNodes("//textarea");
                if (htmlNodeCollection != null)
                {
                    foreach (HtmlNode htmlNode in ((IEnumerable<HtmlNode>)htmlNodeCollection))
                    {
                        if (htmlNode.Attributes["leipiplugins"] != null)
                        {
                            htmlNode.SetAttributeValue("leipiplugins", "textarea");
                            if (htmlNode.Attributes["name"] != null && htmlNode.Attributes["name"].Value != "")
                            {
                                wf_formfieldinfo formFieldInfo = new wf_formfieldinfo();
                                if (htmlNode.Attributes["id"] != null)
                                {
                                    string num2 = htmlNode.Attributes["id"].Value;
                                    if (!string.IsNullOrEmpty(num2) && int.Parse(num2) > 0)
                                    {
                                        if (formServers.GetModelFieldById(num2) != null)
                                        {
                                            formFieldInfo = formServers.GetModelFieldById(num2);
                                        }
                                    }
                                }
                                formFieldInfo.NAME = htmlNode.Attributes["name"].Value;
                                formFieldInfo.PLUGINS = "textarea";
                                if (htmlNode.Attributes["value"] != null)
                                {
                                    formFieldInfo.DEFVALUE = htmlNode.Attributes["value"].Value;
                                }
                                if (formFieldInfo.ID > 0)
                                {
                                    formServers.UpdateField(formFieldInfo);
                                }
                                else
                                {
                                    formFieldInfo.FORMID = model.ID;
                                    formServers.AddField(formFieldInfo);
                                    formFieldInfo.ID = formServers.GetIDByName(formFieldInfo.NAME, model.ID.ToString());
                                }
                                htmlNode.SetAttributeValue("id", formFieldInfo.ID.ToString());
                                num++;
                                if (text != "")
                                {
                                    text += ",";
                                }
                                text += formFieldInfo.ID.ToString();
                            }
                        }
                        else
                        {
                            htmlNode.SetAttributeValue("leipiplugins", "textarea");
                            if (htmlNode.Attributes["title"] != null && htmlNode.Attributes["title"].Value != "")
                            {
                                wf_formfieldinfo formFieldInfo = new wf_formfieldinfo();
                                //SqlParam[] sqlparams = new SqlParam[]
                                //{
                                //        DbHelper.MakeAndWhere("name", htmlNode.Attributes["title"].Value),
                                //        DbHelper.MakeAndWhere("formid", this.id)
                                //};
                                formFieldInfo = formServers.GetModelFieldByName(htmlNode.Attributes["title"].Value, id.Value.ToString());// DbHelper.ExecuteModel<FormFieldInfo>(sqlparams);
                                formFieldInfo.NAME = htmlNode.Attributes["title"].Value;
                                formFieldInfo.DATATYPE = "";
                                formFieldInfo.PLUGINS = "textarea";
                                if (formFieldInfo.ID > 0)
                                {
                                    formServers.UpdateField(formFieldInfo);
                                }
                                else
                                {
                                    formFieldInfo.FORMID = model.ID;
                                    formServers.AddField(formFieldInfo);
                                    formFieldInfo.ID = formServers.GetIDByName(formFieldInfo.NAME, model.ID.ToString());
                                }
                                htmlNode.SetAttributeValue("name", formFieldInfo.NAME);
                                htmlNode.SetAttributeValue("id", formFieldInfo.ID.ToString());
                                num++;
                                if (text != "")
                                {
                                    text += ",";
                                }
                                text += formFieldInfo.ID.ToString();
                            }
                        }
                        if (htmlNode.Attributes["value"] == null)
                        {
                            htmlNode.SetAttributeValue("value", "");
                        }
                        if (htmlNode.Attributes["orgrich"] == null)
                        {
                            htmlNode.SetAttributeValue("orgrich", "0");
                        }
                        if (htmlNode.Attributes["orgfontsize"] == null)
                        {
                            htmlNode.SetAttributeValue("orgfontsize", "");
                        }
                        if (htmlNode.Attributes["orgwidth"] == null)
                        {
                            htmlNode.SetAttributeValue("orgwidth", "300");
                        }
                        if (htmlNode.Attributes["orgheight"] == null)
                        {
                            htmlNode.SetAttributeValue("orgheight", "80");
                        }
                    }
                }
                model.CONTENT = htmlDocument.DocumentNode.InnerHtml;
                model.FIELDS = num;
                string text2 = "";
                if (!string.IsNullOrEmpty(model.FIELDLIST))
                {
                    foreach (int num3 in FPUtils.SplitInt(model.FIELDLIST))
                    {
                        if (!FPUtils.InArray(num3, text))
                        {
                            if (text2 != "")
                            {
                                text2 += ",";
                            }
                            text2 += num3.ToString();
                        }
                    }
                    if (text2 != "")
                    {
                        formServers.DelField(text2);
                    }
                }
                model.FIELDLIST = text;
                formServers.Update(model);
                ViewBag.msg = "设计表单信息成功！";
            }
            return View();
        }

        // GET: System/Form/Edit/5
        public ActionResult Formview(int? id)
        {
            ViewBag.Content = "";
            if (id != null)
            {
                ViewBag.Title = "预览表单";
                wf_forminfo model = GetModel(id.Value.ToString());
                ViewBag.Content = model.CONTENT;
            }
            else
            {
                ViewBag.Title = "新建表单";
            }
            return View();
        }

        // GET: System/Form/Edit/5
        public ActionResult Formimport(int? id)
        {
            var model = new wf_forminfo();
            if (id != null)
            {
                ViewBag.Title = "导入表单";
                model = GetModel(id.Value.ToString());
            }
            else
            {
                ViewBag.Title = "新建表单";
            }
            return View(model);
        }

        #region //GetData
        public string GetTableJsonResult()
        {
            var list = formServers.GetData();
            return JsonConvert.SerializeObject(list);
        }

        public wf_forminfo GetModel(string id)
        {
            var model = formServers.GetModelById(id);
            return model;
        }

        #endregion //GetData
    }
}